﻿using GameFramework;
using GameFramework.Event;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace GFTest
{
    /// <summary>
    /// 界面3d角色摄像机
    /// </summary>
    [XLua.LuaCallCSharp, DisallowMultipleComponent]
    public class FakeCameraComponent : GameFrameworkComponent
    {
        /// <summary>
        /// 摄像机
        /// </summary>
        [SerializeField]
        private Camera m_Camera;

        /// <summary>
        /// 图像投射区域
        /// </summary>
        [SerializeField]
        private UIViewport m_UIViewport;

        /// <summary>
        /// 摄像机默认位置
        /// </summary>
        private Vector3 m_DefaultPosition;

        /// <summary>
        /// 摄像机默认角度
        /// </summary>
        private Vector3 m_DefaultRotation;

        /// <summary>
        /// 摄像机默认视野
        /// </summary>
        private float m_DefaultField;

        /// <summary>
        /// 实体 资源Id
        /// </summary>
        private int m_EntityType;

        /// <summary>
        /// 实体 sid
        /// </summary>
        private int m_EntitySid;

        /// <summary>
        /// 实体
        /// </summary>
        private UnityGameFramework.Runtime.Entity m_Entity;

        /// <summary>
        /// 缓存transform
        /// </summary>
        private Transform m_EntityTransform;

        /// <summary>
        /// 开始
        /// </summary>
        private void Start()
        {
            if (m_Camera == null)
            {
                Log.Error("FakeCameraComponent init failed. Camera is null!");
                return;
            }
            if (m_UIViewport == null)
            {
                Log.Error("FakeCameraComponent init failed. UIViewport is null!");
                return;
            }
            m_DefaultPosition = m_Camera.transform.position;
            m_DefaultRotation = m_Camera.transform.eulerAngles;
            m_DefaultField = m_Camera.fieldOfView;

            GameEntry.Event.Subscribe(StartChangeSceneEventArgs.EventId, OnStartChangeScene);
            GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
            GameEntry.Event.Subscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
        }

        /// <summary>
        /// 销毁
        /// </summary>
        private void OnDestroy()
        {
            GameEntry.Event.Unsubscribe(StartChangeSceneEventArgs.EventId, OnStartChangeScene);
            GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
            GameEntry.Event.Unsubscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
        }

        /// <summary>
        /// 设置摄像机位置
        /// </summary>
        /// <param name="position">位置</param>
        public void SetCameraPosition(Vector3 position)
        {
            m_Camera.transform.position = position;
        }

        /// <summary>
        /// 设置摄像机角度
        /// </summary>
        /// <param name="eulerAngles">角度</param>
        public void SetCameraRotation(Vector3 eulerAngles)
        {
            m_Camera.transform.eulerAngles = eulerAngles;
        }

        /// <summary>
        /// 设置摄像机视野
        /// </summary>
        /// <param name="value">视野</param>
        public void SetFieldOfView(float value)
        {
            m_Camera.fieldOfView = value;
        }

        /// <summary>
        /// 设置摄像机投影边界
        /// </summary>
        /// <param name="topLeft">左上角</param>
        /// <param name="bottomRight">右下角</param>
        public void SetViewportBorder(Transform topLeft, Transform bottomRight)
        {
            m_UIViewport.topLeft = topLeft;
            m_UIViewport.bottomRight = bottomRight;
        }

        /// <summary>
        /// 重置摄像机
        /// </summary>
        public void Reset()
        {
            m_Camera.transform.position = m_DefaultPosition;
            m_Camera.transform.eulerAngles = m_DefaultRotation;
            m_Camera.fieldOfView = m_DefaultField;
            m_UIViewport.topLeft = null;
            m_UIViewport.bottomRight = null;
        }

        /// <summary>
        /// 显示实体
        /// </summary>
        /// <param name="type">模型id</param>
        public void ShowEntity(int type)
        {
            if (m_UIViewport.topLeft == null || m_UIViewport.bottomRight == null)
            {
                Log.Warning("Fake camera component show entity failed, UIViewport topLeft or bottomRight is null.");
                return;
            }
            //m_EntitySid = GameEntry.Entity.GenerateSerialId();

            //FakeCharacterData fakeCharacter = ReferencePool.Acquire<FakeCharacterData>();
            //fakeCharacter.Id = m_EntitySid;
            //fakeCharacter.Type = type;
            //fakeCharacter.Position = Vector3.zero;
            //fakeCharacter.Rotation = Quaternion.identity;
            //GameEntry.Entity.ShowFakeCharacter(fakeCharacter);

            m_Camera.gameObject.SetActive(true);
            m_EntityType = type;
        }

        /// <summary>
        /// 隐藏实体
        /// </summary>
        public void HideEntity()
        {
            m_Camera.gameObject.SetActive(false);
            GameEntry.Entity.HideEntity(m_Entity);
            m_EntityType = 0;
            m_Entity = null;
        }

        /// <summary>
        /// 旋转实体
        /// </summary>
        /// <param name="y">角度</param>
        public void RotateEntity(float y)
        {
            if (m_Entity != null)
            {
                m_EntityTransform.eulerAngles = new Vector3(0, m_EntityTransform.eulerAngles.y + y, 0);
            }
        }

        private void OnStartChangeScene(object sender, GameEventArgs e)
        {
            m_Entity = null;
        }

        private void OnShowEntitySuccess(object sender, GameEventArgs e)
        {
            if (m_EntityType != 0)
            {
                //ShowEntitySuccessEventArgs ne = (ShowEntitySuccessEventArgs)e;
                //FakeCharacterData data = ne.UserData as FakeCharacterData;
                //if (data != null && data.Id == m_EntitySid)
                //{
                //    m_Entity = ne.Entity;
                //    m_Entity.transform.SetLayerRecursively(Constant.Layer.FakeCameraLayerId);
                //    m_EntityTransform = m_Entity.transform;
                //}
            }
        }

        private void OnShowEntityFailure(object sender, GameEventArgs e)
        {
            if (m_EntityType != 0)
            {
                ShowEntityFailureEventArgs ne = (ShowEntityFailureEventArgs)e;
                if (ne.EntityId == m_EntitySid)
                {
                    Log.Warning("Fake camera component show entity failed, " + ne.ErrorMessage);
                    m_EntityType = 0;
                }
            }
        }
    }
}
